﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class CreateTestPageTwo : System.Web.UI.Page
{
    DataTable dt = new DataTable();
    //public static string testId,copyTestId;
    public static Dictionary<int, string> questionSelected = new Dictionary<int, string>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gvQuestion.DataSource = null;
            gvQuestion.DataBind();
            bindGrid();
            questionSelected.Clear();
            //testId = Request.QueryString["TestId"];
            //copyTestId = Request.QueryString["CopyTestId"];
            if ((Request.QueryString["TestId"] != null)&&(Request.QueryString["TestId"] != ""))
                setControlValues();
            if ((Request.QueryString["CopyTestId"] != null)&&(Request.QueryString["CopyTestId"] != ""))
                setCopyControlValues();
            if (Request.QueryString["previousPage"] == "page3")
                setAllValue();
        }
    }

    private void setCopyControlValues()
    {
        DataTable dtTest = new DataTable();
        dtTest = AdminCreateTestDAL.GetTestInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["CopyTestId"]));
        txtCuttOffmarks.Text = dtTest.Rows[0][8].ToString();
        txtTotalNoOfQuestionSelected.Text = dtTest.Rows[0][4].ToString();

        DataTable dtTestTopic = new DataTable();
        dtTestTopic = AdminCreateTestDAL.GetTopicInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["CopyTestId"]));
        string topicId = "";
        for (int i = 0; i < dtTestTopic.Rows.Count; i++)
        {
            topicId += dtTestTopic.Rows[i][1].ToString()+"|";
        }
        string s = string.Empty;
        DataTable dtGrid = new DataTable();
        if (topicId == Information.topicId)
        {
            dtGrid = AdminCreateTestDAL.GetQuestionAllInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["CopyTestId"]));
            gvQuestion.DataSource = dtGrid;
            gvQuestion.DataBind();
            gvQuestion.Columns[1].Visible = true;
        }
        else
        {
            dtGrid = AdminCreateTestDAL.GetQuestionBySubjectTopic(Convert.ToInt32(Information.subjectId), Information.topicId);
            gvQuestion.DataSource = dtGrid;
            gvQuestion.DataBind();
            gvQuestion.Columns[1].Visible = true;
        }

        DataTable dtQues = new DataTable();
        dtQues = AdminCreateTestDAL.GetQuestionInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["CopyTestId"]));

        //DataTable dtQuesSeq = new DataTable();
        //dtQuesSeq.Columns.Add("QuestionId", typeof(System.Int32));
        //for (int i = 0; i < dtGrid.Rows.Count; i++)
        //{
        //    for (int j = 0; j < dtQues.Rows.Count; j++)
        //    {
        //        if (dtGrid.Rows[i][2].ToString().Trim() == dtQues.Rows[j][0].ToString().Trim())
        //        {
        //            dtQuesSeq.Rows.Add(dtGrid.Rows[i][2]);
        //        }
        //    }
        //}

        //for (int i = 10, k = 0; i < dtQuesSeq.Rows.Count; i+=10)
        //{
        //    if (i % 10 == 0)
        //    {
        //        k++;
        //        s = string.Empty;
        //        for (int j = i; j < i+10; j++)
        //        {
        //            s += dtQuesSeq.Rows[j][0].ToString() + "|";
        //        }
        //        //s = s.Remove(s.Length - 1);
        //        questionSelected.Add(k, s);
        //    }    
        //}

        for (int i = 0; i < gvQuestion.Rows.Count; i++)
        {
            for (int j = 0; j < dtQues.Rows.Count; j++)
            {
                if (gvQuestion.DataKeys[i].Value.ToString() == dtQues.Rows[j][0].ToString())
                {
                    CheckBox cb = (CheckBox)gvQuestion.Rows[i].FindControl("cb");
                    cb.Checked = true;
                }
            }
        }
    }

    private void setControlValues()
    {
        string s = string.Empty;
        DataTable dtTest = new DataTable();
        dtTest = AdminCreateTestDAL.GetTestInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["TestId"]));
        txtCuttOffmarks.Text = dtTest.Rows[0][8].ToString();
        txtTotalNoOfQuestionSelected.Text = dtTest.Rows[0][4].ToString();

        DataTable dtTestTopic = new DataTable();
        dtTestTopic = AdminCreateTestDAL.GetTopicInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["TestId"]));
        string topicId = "";
        for (int i = 0; i < dtTestTopic.Rows.Count; i++)
        {
            topicId += dtTestTopic.Rows[i][1].ToString()+"|";
        }

        DataTable dtGrid = new DataTable();
        if (topicId == Information.topicId)
        {
            dtGrid = AdminCreateTestDAL.GetQuestionAllInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["TestId"]));
            gvQuestion.DataSource = dtGrid;
            gvQuestion.DataBind();
            gvQuestion.Columns[1].Visible = true;
        }
        else
        {
            dtGrid = AdminCreateTestDAL.GetQuestionBySubjectTopic(Convert.ToInt32(Information.subjectId), Information.topicId);
            gvQuestion.DataSource = dtGrid;
            gvQuestion.DataBind();
            gvQuestion.Columns[1].Visible = true;
        }

        DataTable dtQues = new DataTable();
        dtQues = AdminCreateTestDAL.GetQuestionInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["TestId"]));

        //DataTable dtQuesSeq = new DataTable();
        //dtQuesSeq.Columns.Add("QuestionId", typeof(System.Int32));
        //for (int i = 0; i < dtGrid.Rows.Count; i++)
        //{
        //    for (int j = 0; j < dtQues.Rows.Count; j++)
        //    {
        //        if (dtGrid.Rows[i][2].ToString().Trim() == dtQues.Rows[j][0].ToString().Trim())
        //        {
        //            dtQuesSeq.Rows.Add(dtGrid.Rows[i][2]);
        //        }
        //    }
        //}

        //for (int i = 10, k = 0; i < dtQuesSeq.Rows.Count; i += 10)
        //{
        //    if (i % 10 == 0)
        //    {
        //        k++;
        //        s = string.Empty;
        //        for (int j = i; j < i + 10; j++)
        //        {
        //            s += dtQuesSeq.Rows[j][0].ToString() + "|";
        //        }
        //        //s = s.Remove(s.Length - 1);
        //        questionSelected.Add(k, s);
        //    }
        //}
        for (int i = 0; i < gvQuestion.Rows.Count; i++)
        {
            for (int j = 0; j < dtQues.Rows.Count; j++)
            {
                if (gvQuestion.DataKeys[i].Value.ToString() == dtQues.Rows[j][0].ToString())
                {
                    CheckBox cb = (CheckBox)gvQuestion.Rows[i].FindControl("cb");
                    cb.Checked = true;
                }
            }
        }
    }

    private void setAllValue()
    {
        txtCuttOffmarks.Text = Information.CuttoffmarksPage2.ToString();
        txtTotalNoOfQuestionSelected.Text = Information.totalNoOfQuestionSelectedPage2.ToString();
        string ques = Information.question;
        string[] allQuestion = ques.Split('|');
        //bindGrid();
        for (int i = 0; i < gvQuestion.Rows.Count; i++)
        {
            for(int j=0;j<allQuestion.Length;j++)
            {
                if (gvQuestion.DataKeys[i].Value.ToString() == allQuestion[j])
                {
                    CheckBox cb = (CheckBox)gvQuestion.Rows[i].FindControl("cb");
                    cb.Checked = true;
                }
            }
        }
    }

    private void bindGrid()
    {
        dt = AdminCreateTestDAL.GetQuestionBySubjectTopic(Convert.ToInt32(Information.subjectId), Information.topicId);
        gvQuestion.DataSource = dt;
        gvQuestion.DataBind();
        gvQuestion.Columns[1].Visible = true;
        //gvQuestion.Columns[3].Visible = false;
    }

    protected void btnTotalQuestion_Click(object sender, EventArgs e)
    {
        int count = 0;
        foreach (GridViewRow row in gvQuestion.Rows)
        {
            // Access the CheckBox
            CheckBox cb = (CheckBox)row.FindControl("cb");
            if (cb.Checked == true)
            {
                count++;
            }
        }

        foreach (var s in questionSelected)
        {
            string str = s.Value;
            string[] str1 = str.Split('|');
            count += str1.Length - 1;
        }

        txtTotalNoOfQuestionSelected.Text = count.ToString();
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        if((Request.QueryString["TestId"]!=null)&&(Request.QueryString["TestId"]!=""))
            Response.Redirect("~/Admin/CreateTestPageOne.aspx?previousPage=page2&TestId=" + Request.QueryString["TestId"]);
        else if ((Request.QueryString["CopyTestId"] != null)&&(Request.QueryString["CopyTestId"]!=""))
            Response.Redirect("~/Admin/CreateTestPageOne.aspx?previousPage=page2&CopyTestId=" + Request.QueryString["CopyTestId"]);
        else
            Response.Redirect("~/Admin/CreateTestPageOne.aspx?previousPage=page2");
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtTotalNoOfQuestionSelected.Text = "";
        txtCuttOffmarks.Text = "";
        foreach (GridViewRow row in gvQuestion.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("cb");
            if (cb.Checked == true)
            {
                cb.Checked = false;
            }
        }
        questionSelected.Clear();
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        bool flag=false;
        //foreach (GridViewRow row in gvQuestion.Rows)
        //{
        //    bool result = ((CheckBox)row.FindControl("cb")).Checked;
        //    if (result)
        //    {
        //        flag = true;
        //        break;
        //    }
        //}

        int count = 0;
        foreach (GridViewRow row in gvQuestion.Rows)
        {
            // Access the CheckBox
            CheckBox cb = (CheckBox)row.FindControl("cb");
            if (cb.Checked == true)
            {
                count++;
            }
        }

        foreach (var s in questionSelected)
        {
            string str = s.Value;
            string[] str1 = str.Split('|');
            count += str1.Length - 1;
        }

        if (count == 0)
            flag = false;
        else
            flag = true;

        if (validation())
        {
            if (flag)
            {
                string ques = "";
                Information.totalNoOfQuestionSelectedPage2 = Convert.ToInt32(txtTotalNoOfQuestionSelected.Text);
                Information.CuttoffmarksPage2 = Convert.ToInt32(txtCuttOffmarks.Text);
                DataKey key;
                foreach (GridViewRow row in gvQuestion.Rows)
                {
                    bool result = ((CheckBox)row.FindControl("cb")).Checked;
                    if (result)
                    {
                        key = gvQuestion.DataKeys[row.RowIndex];
                        ques += key.Value.ToString() + "|";
                    }
                }

                foreach (var s in questionSelected)
                {
                    string str = s.Value;
                    ques += str;
                }

                Information.question = ques;
                if(Request.QueryString["TestId"]!=null)
                    Response.Redirect("~/Admin/CreateTestPageThree.aspx?fromPage=page2&TestId=" + Request.QueryString["TestId"]);
                else if (Request.QueryString["CopyTestId"] != null)
                    Response.Redirect("~/Admin/CreateTestPageThree.aspx?fromPage=page2&CopyTestId=" + Request.QueryString["CopyTestId"]);
                else
                    Response.Redirect("~/Admin/CreateTestPageThree.aspx?fromPage=page2");
            }
            else
                lablError.Text = "Atleast select one question";
        }
    }

    private bool validation()
    {
        if (txtTotalNoOfQuestionSelected.Text == "")
        {
            lablError.Text = "Please prees total selected questions button";
            return false;
        }
        if(txtCuttOffmarks.Text=="")
        {
            lablError.Text = "Please enter cutoff marks";
            return false;
        }
        if (Convert.ToInt32(txtCuttOffmarks.Text) < 0)
        {
            lablError.Text = "Cutoff marks can not be negative";
            return false;
        }
        if (Convert.ToInt32(txtCuttOffmarks.Text) > Convert.ToInt32(txtTotalNoOfQuestionSelected.Text))
        {
            lablError.Text = "CutOff marks cannot be greater than total no of question selected";
            return false;
        }
        return true;
    }
    protected void gvQuestion_SelectedIndexChanged(object sender, EventArgs e)
    {

    }


    protected void HeaderCheckedChanged(object sender, EventArgs e)//this is for header checkbox changed event
    {
        CheckBox cbSelectedHeader = (CheckBox)gvQuestion.HeaderRow.FindControl("cbhead");//if u checked header checkbox automatically all the check boxes will be checked,viseversa
        foreach (GridViewRow row in gvQuestion.Rows)
        {
            CheckBox cbSelected = (CheckBox)row.FindControl("cb");
            if (cbSelectedHeader.Checked == true)
            {
                cbSelected.Checked = true;

            }
            else
            {
                cbSelected.Checked = false;
            }
        }
    }


    protected void gvQuestion_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        string ques = "";
        foreach (GridViewRow row in gvQuestion.Rows)
        {
            bool result = ((CheckBox)row.FindControl("cb")).Checked;
            if (result)
            {
                ques += gvQuestion.DataKeys[row.RowIndex].Value.ToString() + "|";
            }
        }


        questionSelected.Add((Convert.ToInt32(gvQuestion.PageIndex)), ques);
        gvQuestion.PageIndex = e.NewPageIndex;

        if((Request.QueryString["TestId"]==null)&&(Request.QueryString["CopyTestId"]==null))
            bindGrid();
        else if (Request.QueryString["TestId"] != null)
        {
            DataTable dtTest = new DataTable();
            dtTest = AdminCreateTestDAL.GetTestInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["TestId"]));

            DataTable dtTestTopic = new DataTable();
            dtTestTopic = AdminCreateTestDAL.GetTopicInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["TestId"]));
            string topicId = "";
            for (int i = 0; i < dtTestTopic.Rows.Count; i++)
            {
                topicId += dtTestTopic.Rows[i][1].ToString();
            }

            DataTable dtGrid = new DataTable();
            dtGrid = AdminCreateTestDAL.GetQuestionAllInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["TestId"]));
            gvQuestion.DataSource = dtGrid;
            gvQuestion.DataBind();
            gvQuestion.Columns[1].Visible = true;
        }
        else if (Request.QueryString["CopyTestId"] != null)
        {
            DataTable dtTest = new DataTable();
            dtTest = AdminCreateTestDAL.GetTestInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["CopyTestId"]));

            DataTable dtTestTopic = new DataTable();
            dtTestTopic = AdminCreateTestDAL.GetTopicInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["CopyTestId"]));
            string topicId = "";
            for (int i = 0; i < dtTestTopic.Rows.Count; i++)
            {
                topicId += dtTestTopic.Rows[i][1].ToString();
            }

            DataTable dtGrid = new DataTable();
            dtGrid = AdminCreateTestDAL.GetQuestionAllInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["CopyTestId"]));
            gvQuestion.DataSource = dtGrid;
            gvQuestion.DataBind();
            gvQuestion.Columns[1].Visible = true;
        }
    }
    protected void gvQuestion_PageIndexChanged(object sender, EventArgs e)
    {

        #region testId And CopyTestId is null
        if ((Request.QueryString["TestId"] == null) && (Request.QueryString["CopyTestId"] == null))
        {
            bool isComingAgain = true;

            foreach (var s in questionSelected)
            {
                if (s.Key == (Convert.ToInt32(gvQuestion.PageIndex)))
                {
                    isComingAgain = false;
                    break;
                }
            }

            if ((Request.QueryString["previousPage"] != "page3") || ((Request.QueryString["previousPage"] == "page3") && (isComingAgain == false)))
            {
                string questions = "";
                string[] question;
                bool flag = true;

                foreach (var s in questionSelected)
                {
                    if (s.Key == (Convert.ToInt32(gvQuestion.PageIndex)))
                    {
                        questions = s.Value;
                        flag = false;
                        break;
                    }
                }
                if (flag == false)
                {
                    question = questions.Split('|');

                    for (int i = 0; i < gvQuestion.Rows.Count; i++)
                    {
                        for (int j = 0; j < question.Length; j++)
                        {
                            if (gvQuestion.DataKeys[i].Value.ToString() == question[j])
                            {
                                CheckBox cb = (CheckBox)gvQuestion.Rows[i].FindControl("cb");
                                cb.Checked = true;
                            }
                        }
                    }

                    if (questionSelected.ContainsKey((Convert.ToInt32(gvQuestion.PageIndex))))
                        questionSelected.Remove((Convert.ToInt32(gvQuestion.PageIndex)));
                }
            }

            else if ((Request.QueryString["previousPage"] == "page3") && (isComingAgain == true))
            {
                string ques = Information.question;
                string[] allQuestion = ques.Split('|');
                for (int i = 0; i < gvQuestion.Rows.Count; i++)
                {
                    for (int j = 0; j < allQuestion.Length; j++)
                    {
                        if (gvQuestion.DataKeys[i].Value.ToString() == allQuestion[j])
                        {
                            CheckBox cb = (CheckBox)gvQuestion.Rows[i].FindControl("cb");
                            cb.Checked = true;
                        }
                    }
                }
                if (questionSelected.ContainsKey((Convert.ToInt32(gvQuestion.PageIndex))))
                    questionSelected.Remove((Convert.ToInt32(gvQuestion.PageIndex)));

            }
        }
        #endregion

        #region testId is not null
        else if ((Request.QueryString["TestId"] != null) && (Request.QueryString["TestId"] != ""))
        {
            bool isComingAgain = true;

            foreach (var s in questionSelected)
            {
                if (s.Key == (Convert.ToInt32(gvQuestion.PageIndex)))
                {
                    isComingAgain = false;
                    break;
                }
            }

            if ((Request.QueryString["previousPage"] != "page3") && (isComingAgain == true))
            {
                DataTable dtQues = new DataTable();
                dtQues = AdminCreateTestDAL.GetQuestionInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["TestId"]));

                for (int i = 0; i < gvQuestion.Rows.Count; i++)
                {
                    for (int j = 0; j < dtQues.Rows.Count; j++)
                    {
                        if (gvQuestion.DataKeys[i].Value.ToString() == dtQues.Rows[j][0].ToString())
                        {
                            CheckBox cb = (CheckBox)gvQuestion.Rows[i].FindControl("cb");
                            cb.Checked = true;
                        }
                    }
                }

                if (questionSelected.ContainsKey((Convert.ToInt32(gvQuestion.PageIndex))))
                    questionSelected.Remove((Convert.ToInt32(gvQuestion.PageIndex)));

            }

            else if (((Request.QueryString["previousPage"] != "page3") && (isComingAgain == false)) || ((Request.QueryString["previousPage"] == "page3") && (isComingAgain == false)))
            {
                string questions = "";
                string[] question;
                bool flag = true;

                foreach (var s in questionSelected)
                {
                    if (s.Key == (Convert.ToInt32(gvQuestion.PageIndex)))
                    {
                        questions = s.Value;
                        flag = false;
                        break;
                    }
                }
                if (flag == false)
                {
                    question = questions.Split('|');

                    for (int i = 0; i < gvQuestion.Rows.Count; i++)
                    {
                        for (int j = 0; j < question.Length; j++)
                        {
                            if (gvQuestion.DataKeys[i].Value.ToString() == question[j])
                            {
                                CheckBox cb = (CheckBox)gvQuestion.Rows[i].FindControl("cb");
                                cb.Checked = true;
                            }
                        }
                    }

                    if (questionSelected.ContainsKey((Convert.ToInt32(gvQuestion.PageIndex))))
                        questionSelected.Remove((Convert.ToInt32(gvQuestion.PageIndex)));
                }
            }

            else if ((Request.QueryString["previousPage"] == "page3") && (isComingAgain == true))
            {
                string ques = Information.question;
                string[] allQuestion = ques.Split('|');
                for (int i = 0; i < gvQuestion.Rows.Count; i++)
                {
                    for (int j = 0; j < allQuestion.Length; j++)
                    {
                        if (gvQuestion.DataKeys[i].Value.ToString() == allQuestion[j])
                        {
                            CheckBox cb = (CheckBox)gvQuestion.Rows[i].FindControl("cb");
                            cb.Checked = true;
                        }
                    }
                }
                if (questionSelected.ContainsKey((Convert.ToInt32(gvQuestion.PageIndex))))
                    questionSelected.Remove((Convert.ToInt32(gvQuestion.PageIndex)));

            }
        }
        #endregion

        #region CopyTestId is not null 
        else if ((Request.QueryString["CopyTestId"] != null) && (Request.QueryString["CopyTestId"] != ""))
        {
            bool isComingAgain = true;

            foreach (var s in questionSelected)
            {
                if (s.Key == (Convert.ToInt32(gvQuestion.PageIndex)))
                {
                    isComingAgain = false;
                    break;
                }
            }

            if ((Request.QueryString["previousPage"] != "page3") && (isComingAgain == true))
            {
                DataTable dtQues = new DataTable();
                dtQues = AdminCreateTestDAL.GetQuestionInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["CopyTestId"]));

                for (int i = 0; i < gvQuestion.Rows.Count; i++)
                {
                    for (int j = 0; j < dtQues.Rows.Count; j++)
                    {
                        if (gvQuestion.DataKeys[i].Value.ToString() == dtQues.Rows[j][0].ToString())
                        {
                            CheckBox cb = (CheckBox)gvQuestion.Rows[i].FindControl("cb");
                            cb.Checked = true;
                        }
                    }
                }

                if (questionSelected.ContainsKey((Convert.ToInt32(gvQuestion.PageIndex))))
                    questionSelected.Remove((Convert.ToInt32(gvQuestion.PageIndex)));

            }

            else if (((Request.QueryString["previousPage"] != "page3") && (isComingAgain == false)) || ((Request.QueryString["previousPage"] == "page3") && (isComingAgain == false)))
            {
                string questions = "";
                string[] question;
                bool flag = true;

                foreach (var s in questionSelected)
                {
                    if (s.Key == (Convert.ToInt32(gvQuestion.PageIndex)))
                    {
                        questions = s.Value;
                        flag = false;
                        break;
                    }
                }
                if (flag == false)
                {
                    question = questions.Split('|');

                    for (int i = 0; i < gvQuestion.Rows.Count; i++)
                    {
                        for (int j = 0; j < question.Length; j++)
                        {
                            if (gvQuestion.DataKeys[i].Value.ToString() == question[j])
                            {
                                CheckBox cb = (CheckBox)gvQuestion.Rows[i].FindControl("cb");
                                cb.Checked = true;
                            }
                        }
                    }

                    if (questionSelected.ContainsKey((Convert.ToInt32(gvQuestion.PageIndex))))
                        questionSelected.Remove((Convert.ToInt32(gvQuestion.PageIndex)));
                }
            }

            else if ((Request.QueryString["previousPage"] == "page3") && (isComingAgain == true))
            {
                string ques = Information.question;
                string[] allQuestion = ques.Split('|');
                for (int i = 0; i < gvQuestion.Rows.Count; i++)
                {
                    for (int j = 0; j < allQuestion.Length; j++)
                    {
                        if (gvQuestion.DataKeys[i].Value.ToString() == allQuestion[j])
                        {
                            CheckBox cb = (CheckBox)gvQuestion.Rows[i].FindControl("cb");
                            cb.Checked = true;
                        }
                    }
                }
                if (questionSelected.ContainsKey((Convert.ToInt32(gvQuestion.PageIndex))))
                    questionSelected.Remove((Convert.ToInt32(gvQuestion.PageIndex)));

            }
        }
           


        #endregion

    }

}